<template>
  <div class="password">
    <div class="cu-common-title mb-20">
      <h2 class="bold">修改密码</h2>
    </div>
    <el-row>
      <el-col :span="16">
        <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="100px">
          <el-form-item label="旧密码" prop="oldPassword">
            <el-input v-model="dataForm.oldPassword" placeholder="旧密码" show-password />
          </el-form-item>
          <el-form-item label="新密码" prop="password">
            <el-input v-model="dataForm.password" placeholder="新密码" show-password />
          </el-form-item>
          <el-form-item label="重复密码" prop="password2">
            <el-input v-model="dataForm.password2" placeholder="重复密码" show-password />
          </el-form-item>
          <el-form-item label="验证码" prop="code">
            <el-col :span="17">
              <el-input v-model="dataForm.code" placeholder="验证码"></el-input>
            </el-col>
            <el-col :span="6" :offset="1" style="height:32px">
              <img id="imgcode" alt="点击切换验证码" title="点击切换验证码" :src="define.comUrl + imgUrl" @click="changeImg" />
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="dataFormSubmit()">保 存</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import md5 from 'js-md5'
import { UpdatePassword } from '@/api/permission/userSetting'
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('新密码不能为空'))
      } else {
        // 至少8位，包含大小写、数字和特殊字符
        const specialCharsRegex = /[!@#$%&'()*+,\-./:;<=>?@\[\\\]^_`{|}~]/
        const passIsMatchRegex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).+$/
        // 检查密码长度
        if (value.Length < 8 || value.Length > 32) {
          return false
        }
        // 检查是否包含大写字母
        // 检查是否包含小写字母
        // 检查是否包含数字
        if (!passIsMatchRegex.test(value)) {
          callback(new Error('至少8位，包含大小写、数字和特殊字符'))
        }
        // 检查是否包含特殊字符
        if (!specialCharsRegex.test(value)) {
          callback(new Error('检查是否包含特殊字符'))
        }
        // 检查是否有连续超过 3 个的相同字符
        for (let i = 0; i < value.Length - 2; i++) {
          callback(new Error('连续超过3个的相同字符'))
        }
        if (this.dataForm.password2 !== '') {
          this.$refs.dataForm.validateField('password2')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.dataForm.password) {
        callback(new Error('两次密码输入不一致'))
      } else {
        callback()
      }
    }
    return {
      dataForm: {
        oldPassword: '',
        password: '',
        password2: '',
        code: ''
      },
      imgUrl: '',
      timestamp: '',
      dataRule: {
        oldPassword: [{ required: true, message: '旧密码不能为空', trigger: 'blur' }],
        password: [{ required: true, validator: validatePass, trigger: 'blur' }],
        password2: [{ required: true, message: '重复密码不能为空', trigger: 'blur' }, { validator: validatePass2, trigger: 'blur' }],
        code: [{ required: true, message: '验证码不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.changeImg()
  },
  methods: {
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          let query = {
            oldPassword: md5(this.dataForm.oldPassword),
            password: md5(this.dataForm.password),
            code: this.dataForm.code,
            timestamp: this.timestamp
          }
          UpdatePassword(query)
            .then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.$store.dispatch('user/resetToken').then(() => {
                    this.$router.push(`/login`)
                  })
                }
              })
            })
            .catch(() => {
              this.changeImg()
            })
        }
      })
    },
    changeImg() {
      let timestamp = Math.random()
      this.timestamp = timestamp
      this.imgUrl = `/api/file/ImageCode/${timestamp}`
    }
  }
}
</script>
<style lang="scss" scoped>
.password {
  >>> .el-input-group__append {
    padding: 0;
    height: 30px;
  }
  #imgcode {
    width: 100px;
    height: 32px;
    overflow: hidden;
    object-fit: cover;
    cursor: pointer;
    margin: 0;
    padding: 0;
  }
}
</style>
